﻿using ERP.Workflow.Designer.Domain;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.EntityFrameworkCore.Modeling;
using WorkflowEngine.Entitys;

namespace WorkflowEngine.EntityFrameworkCore
{
    public static class WorkflowEngineDbContextModelBuilderExtensions
    {
        public static void Configure(
       [NotNull] this ModelBuilder builder)
        {
            Check.NotNull(builder, nameof(builder));

            builder.Entity<BillCode>(b =>
            {
                b.ToTable(WorkflowEngineConsts.DbTablePrefix + nameof(BillCode), WorkflowEngineConsts.DbSchema);
                b.HasIndex(x => x.Id).IsUnique(true);

                b.ConfigureByConvention();
                b.Property(x => x.MaxValue);
                b.Property(x => x.ObjName).HasMaxLength(100);
                b.Property(x => x.Prefix).HasMaxLength(10);

                b.ApplyObjectExtensionMappings();
            });
            builder.Entity<WorkflowApp>(b =>
            {
                b.ToTable(WorkflowEngineConsts.DbTablePrefix + nameof(WorkflowApp), WorkflowEngineConsts.DbSchema);
                b.HasIndex(x => x.Id).IsUnique(true);

                b.ConfigureByConvention();
                b.Property(x => x.AppCode).HasMaxLength(5);
                b.Property(x => x.AppName).HasMaxLength(20);
                b.Property(x => x.Assembly).HasMaxLength(100);
                b.Property(x => x.Category).HasMaxLength(20);
                b.Property(x => x.Format).HasMaxLength(20);
                b.Property(x => x.Icon).HasMaxLength(20);
                b.Property(x => x.FormType);
                b.Property(x => x.AppType);
                b.HasMany<ObjectApp>(m => m.ObjectApps).WithOne(m=>m.WorkflowApp).HasForeignKey(m=>m.AppId);
                b.ApplyObjectExtensionMappings();
            });

            builder.Entity<ObjectApp>(b =>
            {
                b.ToTable(WorkflowEngineConsts.DbTablePrefix + nameof(ObjectApp), WorkflowEngineConsts.DbSchema);
                b.HasIndex(x => x.Id).IsUnique(true);

                b.ConfigureByConvention();
                b.Property(x => x.Id);
                b.Property(x => x.ObjectType);
                b.Property(x => x.ObjectId);
                b.Property(x => x.TenantId);
                b.Property(x => x.IsShow);
                b.HasOne<WorkflowApp>(M => M.WorkflowApp).WithMany().HasForeignKey(M => M.AppId);
                b.ApplyObjectExtensionMappings();
            });

            builder.Entity<WorkflowInstance>(b =>
            {
                b.ToTable(WorkflowEngineConsts.DbTablePrefix + nameof(WorkflowInstance), WorkflowEngineConsts.DbSchema);
                b.HasIndex(x => x.Id).IsUnique(true);

                b.ConfigureByConvention();
                b.Property(x => x.Id);
                b.Property(x => x.ComplateTime).IsRequired(false);
                b.Property(x => x.DefinitionId);
                b.Property(x => x.CreatorOrgId);
                b.Property(x => x.CreatorOrgName).HasMaxLength(200);
                b.Property(x => x.ParentWorkflowInstanceId);
                b.Property(x => x.WorkflowName);
                b.Property(x => x.WorkflowInstanceStatus);
                b.Property(x => x.LevelType);
                b.Property(x => x.CreatorCnUserName).HasMaxLength(10);
                b.Property(x => x.RefObjName).HasMaxLength(10);
                b.Property(x => x.RefObjId).HasMaxLength(10);
                b.Property(x => x.RefObjGroup).HasMaxLength(10);


                b.HasMany<ActivityInstance>(m => m.ActivityInstances).WithOne(m => m.WorkflowInstance).HasForeignKey(m => m.WorkflowInstanceId);
          
                b.ApplyObjectExtensionMappings();
            });

            builder.Entity<ActivityInstance>(b =>
            {
                b.ToTable(WorkflowEngineConsts.DbTablePrefix + nameof(ActivityInstance), WorkflowEngineConsts.DbSchema);
                b.HasIndex(x => x.Id).IsUnique(true);
                b.Property(x => x.ActivityName).HasMaxLength(50);

                b.OwnsOne(x => x.NodeInfo, nb => {
                    nb.Property(m=>m.Contenteditable).HasColumnName(nameof(FlowNodeExtendValue.Contenteditable));
                    nb.Property(m=>m.Name).HasColumnName(nameof(FlowNodeExtendValue.Name)).HasMaxLength(50);
                    nb.Property(m=>m.Icon).HasColumnName(nameof(FlowNodeExtendValue.Icon)).HasMaxLength(50);
                    nb.Property(m=>m.Rotate).HasColumnName(nameof(FlowNodeExtendValue.Rotate));
                    nb.Property(m=>m.Code).HasColumnName(nameof(FlowNodeExtendValue.Code)).HasMaxLength(50);
                    nb.Property(m=>m.OrderNo).HasColumnName(nameof(FlowNodeExtendValue.OrderNo));
                    nb.Property(m=>m.NodeType).HasColumnName(nameof(FlowNodeExtendValue.NodeType));
                    nb.Property(m=>m.SplitRule).HasColumnName(nameof(FlowNodeExtendValue.SplitRule));
                    nb.Property(m => m.SplitRule).HasColumnName(nameof(FlowNodeExtendValue.SplitRule));
                    nb.Property(m => m.JoinRule).HasColumnName(nameof(FlowNodeExtendValue.JoinRule));
                    nb.Property(m => m.ReposeType).HasColumnName(nameof(FlowNodeExtendValue.ReposeType));
                    nb.Property(m => m.EntyType).HasColumnName(nameof(FlowNodeExtendValue.EntyType));
                    nb.Property(m => m.ExitType).HasColumnName(nameof(FlowNodeExtendValue.ExitType));
                    nb.Property(m => m.ActorAssembly).HasColumnName(nameof(FlowNodeExtendValue.ActorAssembly)).HasMaxLength(100);
                    nb.Property(m => m.CirculatedAssembly).HasColumnName(nameof(FlowNodeExtendValue.CirculatedAssembly));
                    nb.Property(m => m.ModeType).HasColumnName(nameof(FlowNodeExtendValue.ModeType));
                    nb.Property(m => m.HasHistoryCanSkip).HasColumnName(nameof(FlowNodeExtendValue.HasHistoryCanSkip));
                    nb.Property(m => m.NotActorIsCanSkip).HasColumnName(nameof(FlowNodeExtendValue.NotActorIsCanSkip));
                    nb.Property(m => m.NotActorDefault).HasColumnName(nameof(FlowNodeExtendValue.NotActorDefault)).HasMaxLength(50);
                    nb.Property(m => m.IsCanCirculated).HasColumnName(nameof(FlowNodeExtendValue.IsCanCirculated));
                    nb.Property(m => m.IsCanComments).HasColumnName(nameof(FlowNodeExtendValue.IsCanComments));
                    nb.Property(m => m.IsCanRejected).HasColumnName(nameof(FlowNodeExtendValue.IsCanRejected));
                    nb.Property(m => m.IsCanWithdraw).HasColumnName(nameof(FlowNodeExtendValue.IsCanWithdraw));
                    nb.Property(m => m.IsCanChangeCirculated).HasColumnName(nameof(FlowNodeExtendValue.IsCanChangeCirculated));
                    nb.Property(m => m.IsCanEditForm).HasColumnName(nameof(FlowNodeExtendValue.IsCanEditForm));

                    nb.Property(m => m.IsFree).HasColumnName(nameof(FlowNodeExtendValue.IsFree));
                    nb.Property(m => m.IsAppend).HasColumnName(nameof(FlowNodeExtendValue.IsAppend));
                    nb.Property(m => m.IsCanRush).HasColumnName(nameof(FlowNodeExtendValue.IsCanRush));
                     

                });
                b.HasMany<ActivityTaskInstance>(m => m.ActivityTaskInstances)
                .WithOne(m=>m.ActivityInstance)
                .HasForeignKey(m=>m.ActivityInstanceId);

                b.ConfigureByConvention();
                b.ApplyObjectExtensionMappings();
            });


            builder.Entity<ActivityTaskInstance>(b =>
            {
                b.ToTable(WorkflowEngineConsts.DbTablePrefix + nameof(ActivityTaskInstance), WorkflowEngineConsts.DbSchema);
                b.HasIndex(x => x.Id).IsUnique(true);

                b.HasOne<WorkTask>(m => m.WorkTask);
                b.HasOne<WorkflowInstance>(m=>m.WorkflowInstance);
                b.HasOne<ActivityInstance>(m=>m.ActivityInstance);
                b.ConfigureByConvention();
             
                
                b.ApplyObjectExtensionMappings();
            });


            builder.Entity<WorkTask>(b =>
            {
                b.ToTable(WorkflowEngineConsts.DbTablePrefix + nameof(WorkTask), WorkflowEngineConsts.DbSchema);
                b.HasIndex(x => x.Id).IsUnique(true);
              
                b.Property(x => x.TaskName).HasMaxLength(100);
                b.Property(x => x.RefObjName).HasMaxLength(20);
                b.Property(x => x.RefObjId).HasMaxLength(50);
                b.Property(x => x.RefObjGroup).HasMaxLength(50);
               

                b.Property(x => x.Description).HasMaxLength(1000);

                b.Property(x => x.Phone).HasMaxLength(20);
                b.Property(x => x.WorkTel).HasMaxLength(20);
                b.Property(x => x.QQ).HasMaxLength(15);
                b.Property(x => x.WeiXin).HasMaxLength(50);
                b.Property(x => x.Url).HasMaxLength(200);
               

                b.ApplyObjectExtensionMappings();
            });




            builder.TryConfigureObjectExtensions<WorkflowEngineDbContext>();
        }
    }
}
